package com.yikaiye.android.yikaiye.ui.fragment.viewmodel;

import androidx.lifecycle.MutableLiveData;

import com.google.gson.Gson;
import com.yikaiye.android.yikaiye.base.BaseViewModel;
import com.yikaiye.android.yikaiye.config.Constant;
import com.yikaiye.android.yikaiye.di.retrofit.BaseModel;
import com.yikaiye.android.yikaiye.di.retrofit.BaseNetCallback;
import com.yikaiye.android.yikaiye.model.CheckComboResponseModel;
import com.yikaiye.android.yikaiye.model.CheckModel;
import com.yikaiye.android.yikaiye.model.FddModel;
import com.yikaiye.android.yikaiye.model.OrderDataModel;

import java.util.HashMap;
import java.util.Map;

public class OrderStatusViewModel extends BaseViewModel {

    /**
     * 订单列表数据
     */
    public MutableLiveData<OrderDataModel> orderDataModelMutableLiveData = new MutableLiveData<>();
    /**
     * 取消订单成功
     */
    public MutableLiveData<String> orderCancelSuccessMutableLiveData = new MutableLiveData<>();
    /**
     * 取消订单失败
     */
    public MutableLiveData<String> orderCancelErrorMutableLiveData = new MutableLiveData<>();
    /**
     * 查看合同数据
     */
    public MutableLiveData<FddModel> lookViewFddModelMutableLiveData = new MutableLiveData<>();
    /**
     * 查看合同失败
     */
    public MutableLiveData<String> lookViewErrorMutableLiveData = new MutableLiveData<>();
    /**
     * 查询认证结果数据
     */
    public MutableLiveData<FddModel> queryResultFddModelMutableLiveData = new MutableLiveData<>();
    /**
     * 查询认证结果失败
     */
    public MutableLiveData<String> queryResultErrorMutableLiveData = new MutableLiveData<>();
    /**
     * 签署电子协议
     */
    public MutableLiveData<FddModel> signFddModelMutableLiveData = new MutableLiveData<>();
    /**
     * 签署电子协议失败
     */
    public MutableLiveData<String> signErrorMutableLiveData = new MutableLiveData<>();
    /**
     * 实名认证/重新实名认证
     */
    public MutableLiveData<FddModel> verifyFddModelMutableLiveData = new MutableLiveData<>();
    /**
     * 实名认证/重新实名认证失败
     */
    public MutableLiveData<String> verifyErrorMutableLiveData = new MutableLiveData<>();
    /**
     * 检查套餐存在
     */
    public MutableLiveData<CheckComboResponseModel> checkResponseModelMutableLiveData = new MutableLiveData<>();
    /**
     * 检查套餐不存在或报错
     */
    public MutableLiveData<String> checkErrorMutableLiveData = new MutableLiveData<>();

    /**
     * 订单列表
     *
     * @param status 订单状态，0：待付款，1：已付款，2：已取消，3：已退款，4：超时
     */
    public void getOrderList(String status, int page) {
        addSubscribe(mRetrofitHelper.getOrderList(status, page, Constant.PAGE_SIZE).subscribeWith(new BaseNetCallback<OrderDataModel>() {
            @Override
            public void onSuccess(BaseModel<OrderDataModel> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    orderDataModelMutableLiveData.setValue(baseModel.getData());
                } else {
                    mErrorData.setValue(baseModel.getMsg());
                }
            }

            @Override
            public void onError(String message) {
                super.onError(message);
                mErrorData.setValue(message);
            }
        }));
    }

    /**
     * 取消订单
     */
    public void cancelOrder(String id) {
        addSubscribe(mRetrofitHelper.cancelOrder(id).subscribeWith(new BaseNetCallback<String>() {
            @Override
            public void onSuccess(BaseModel<String> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    orderCancelSuccessMutableLiveData.setValue("");
                } else {
                    orderCancelErrorMutableLiveData.setValue(baseModel.getMsg());
                }
            }

            @Override
            public void onError(String message) {
                super.onError(message);
                orderCancelErrorMutableLiveData.setValue(message);
            }
        }));
    }

    /**
     * 查看合同
     */
    public void lookView(String orderNo) {
        addSubscribe(mRetrofitHelper.lookView(orderNo).subscribeWith(new BaseNetCallback<FddModel>() {
            @Override
            public void onSuccess(BaseModel<FddModel> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    lookViewFddModelMutableLiveData.setValue(baseModel.getData());
                } else {
                    lookViewErrorMutableLiveData.setValue(baseModel.getMsg());
                }
            }

            @Override
            public void onError(String message) {
                super.onError(message);
                lookViewErrorMutableLiveData.setValue(message);
            }
        }));
    }

    /**
     * 查看认证结果
     */
    public void queryVerifyResult(String orderNo, int customerType) {
        addSubscribe(mRetrofitHelper.queryVerifyResult(orderNo, customerType).subscribeWith(new BaseNetCallback<FddModel>() {
            @Override
            public void onSuccess(BaseModel<FddModel> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    queryResultFddModelMutableLiveData.setValue(baseModel.getData());
                } else {
                    queryResultErrorMutableLiveData.setValue(baseModel.getMsg());
                }
            }

            @Override
            public void onError(String message) {
                super.onError(message);
                queryResultErrorMutableLiveData.setValue(message);
            }
        }));
    }

    /**
     * 签署电子协议
     */
    public void signContract(String orderNo, int customerType) {
        addSubscribe(mRetrofitHelper.signContract(orderNo, customerType).subscribeWith(new BaseNetCallback<FddModel>() {
            @Override
            public void onSuccess(BaseModel<FddModel> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    signFddModelMutableLiveData.setValue(baseModel.getData());
                } else {
                    signErrorMutableLiveData.setValue(baseModel.getMsg());
                }
            }

            @Override
            public void onError(String message) {
                super.onError(message);
                signErrorMutableLiveData.setValue(message);
            }
        }));
    }

    /**
     * 实名认证
     */
    public void realNameVerify(String orderNo, int customerType) {
        Map<String, Object> map = new HashMap<>();
        map.put("platform", 2);
        map.put("source", 1);
        map.put("orderNo", orderNo);
        map.put("customerType", customerType);
        addSubscribe(mRetrofitHelper.realNameVerify(new Gson().toJson(map)).subscribeWith(new BaseNetCallback<FddModel>() {
            @Override
            public void onSuccess(BaseModel<FddModel> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    verifyFddModelMutableLiveData.setValue(baseModel.getData());
                } else {
                    signErrorMutableLiveData.setValue(baseModel.getMsg());
                }
            }

            @Override
            public void onError(String message) {
                super.onError(message);
                verifyErrorMutableLiveData.setValue(message);
            }
        }));
    }

    /**
     * 重新实名认证
     */
    public void resetVerify(String orderNo, int customerType) {
        Map<String, Object> map = new HashMap<>();
        map.put("platform", 2);
        map.put("source", 1);
        map.put("orderNo", orderNo);
        map.put("customerType", customerType);
        addSubscribe(mRetrofitHelper.resetVerify(new Gson().toJson(map)).subscribeWith(new BaseNetCallback<FddModel>() {
            @Override
            public void onSuccess(BaseModel<FddModel> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    verifyFddModelMutableLiveData.setValue(baseModel.getData());
                } else {
                    verifyErrorMutableLiveData.setValue(baseModel.getMsg());
                }
            }

            @Override
            public void onError(String message) {
                super.onError(message);
                verifyErrorMutableLiveData.setValue(message);
            }
        }));
    }

    /**
     * 检查是否存在此套餐
     */
    public void checkProduct(CheckModel checkModel) {
        addSubscribe(mRetrofitHelper.checkProduct(new Gson().toJson(checkModel)).subscribeWith(new BaseNetCallback<CheckComboResponseModel>() {
            @Override
            public void onSuccess(BaseModel<CheckComboResponseModel> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    checkResponseModelMutableLiveData.setValue(baseModel.getData());
                } else {
                    checkErrorMutableLiveData.setValue(baseModel.getMsg());
                }
            }

            @Override
            public void onError(String message) {
                super.onError(message);
                checkErrorMutableLiveData.setValue(message);
            }
        }));
    }
}
